﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KLADR
{
    public partial class FormSelectKladr : Form
    {
        private string kladrCode;
        private string kladrName;
        private string kladrSocr;
        private bool isChanged;

        public bool IsChanged { get { return isChanged; } }
        public string KladrCode { get { return kladrCode; } }
        public string KladrName { get { return kladrName; } }
        public string KladrSocr { get { return kladrSocr; } }
        public string SetText { set { this.Text = value; } }

        public object DataSource { get { return regionBindingSource.DataSource; } set { regionBindingSource.DataSource = value; } }
        public FormSelectKladr()
        {
            InitializeComponent();
            kladrCode = "";
            kladrName = "";
            kladrSocr = "";
            isChanged = false;
        }

        private void AcceptSelection()
        {
            kladrName = toolStripTextBoxRegion.Text;
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            AcceptSelection();
        }

        private void UpdateText()
        {
            kladrDataSet.kladrRow currow = (regionBindingSource.Current as DataRowView).Row as kladrDataSet.kladrRow;
            try
            {
                kladrName = currow.name.Trim().ToUpper();
                kladrSocr = currow.socr.Trim().ToUpper();
                kladrCode = currow.code;
                toolStripTextBoxRegion.Text = kladrName;
                toolStripTextBoxRegionSocr.Text = kladrSocr;
                isChanged = false;
            }
            catch (Exception)
            {
            }
        }

        private void regionBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            UpdateText();
        }

        private void FormSelectRegion_Load(object sender, EventArgs e)
        {
           // UpdateText();
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            AcceptSelection();
        }

        private void toolStripTextBoxRegion_TextChanged(object sender, EventArgs e)
        {
            isChanged = true;
        }
    }
}
